<?php

/**
 * model_manage
 */

class Model_manage extends YOU {

	public function __construct()
	{
		parent::__construct();
	}

	/** 读取总数 **/
	public function count()
	{
		$row = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_manage`");
		return (int)$row['count'];
	}

	/** 读取列表 **/
	public function getAll($field = "*")
	{
		$manage_lists = $this->db->fetchAll("SELECT {$field} FROM `tbl_manage` ");

		return $manage_lists;
	}

	/** 读取信息 **/
	public function getInfo($manage_id, $field = "*")
	{
		$data = $this->db->fetchRow("SELECT {$field} FROM `tbl_manage` WHERE manage_id='{$manage_id}' ");

		// 发布部门
		if (!empty($data)){
			$user_data = $this->db->fetchRow("SELECT b.staff_name,b.department_id FROM `tbl_user` AS a,`tbl_staff` AS b WHERE a.user_id='{$data['user_id']}' AND a.type_id=b.staff_id   ");


			$department_lists = $this->db->fetchAll("SELECT b.department_name FROM `tbl_appoint` AS a,`tbl_department` AS b WHERE a.department_id=b.department_id AND a.staff_id='{$user_data['type_id']}' ");

			$data['staff_name']      = $user_data['staff_name'];
			$data['department_name'] = $department_data['department_name'];

		}


		return $data;
	}


	/** 添加访问记录 **/
	public function stat($manage_id, $user_id)
	{

		$info = array(
		"manage_id"   => $manage_id,
		"user_id"     => $user_id,
		"status"      => 1,
		"create_time" => date("Y-m-d H:i:s"),
		"update_time" => "0000-00-00 00:00:00",
		);
		$this->db->do_insert("tbl_manage_stat", $info);

	}


	/** 读取访问记录 **/
	public function statLists($manage_id)
	{
		$stat_lists = $this->db->fetchAll("SELECT a.*,b.user_type,b.type_id FROM `tbl_manage_stat` AS a,`tbl_user` AS b WHERE a.user_id=b.user_id AND a.manage_id='{$manage_id}'");
		$count  = 0;
		foreach ($stat_lists as $key => $value){
			$count++;
			// 取得部门
			if ($value['user_type'] != "staff"){
				continue;
			}

			// 取得名字 部门
			$staff_data = $this->db->fetchRow("SELECT a.staff_name,b.department_name FROM `tbl_staff` AS a,`tbl_department` AS b WHERE a.department_id=b.department_id  " );

			$data_lists[$staff_data['department_name']][$staff_data['staff_name']]++;
		}

		$stat['count'] = $count;
		$stat['lists'] = $data_lists;


		return  $stat;

	}

}



?>